package com.weiyuwqj.server.service.push.impl;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.weiyuwqj.api.service.NoticePush;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 2016/3/17.
 */
@Service("noticePushService")
public class PushNoticePushImpl implements NoticePush {
    private static String appId = "X3bGCTDZlg8ES";
    private static String appKey = "Au31ScRtSIA";
    private static String masterSecret = "mmGDV28hUtyKNz";
    private static String url = "http://sdk.open.api.igexin.com/serviceex";
    private static String host = "http://sdk.open.api.igexin.com/apiex.htm";


    @Override
    public void allPush(String title, String text) throws IOException {
            // 新建一个IGtPush实例，传入调用接口网址，appkey和masterSecret
            IGtPush push = new IGtPush(url, appKey, masterSecret);
            push.connect();
            // 新建一个消息类型，根据app推送的话要使用AppMessage
            AppMessage message = new AppMessage();
            // 新建一个推送模版, 以链接模板为例子，就是说在通知栏显示一条含图标、标题等的通知，用户点击可打开您指定的网页
            TransmissionTemplate template = getTemplate(text);
            List<String> appIds = new ArrayList<String>();
            appIds.add(appId);
            // 模板设置好后塞进message里并设置，同时设置推送的app id,还可以配置这条message是否支持离线，以及过期时间等
            message.setData(template);
            message.setAppIdList(appIds);
            message.setOffline(true);
            message.setOfflineExpireTime(1000 * 600);
            // 调用IGtPush实例的pushMessageToApp接口，参数就是上面我们配置的message
            IPushResult ret = push.pushMessageToApp(message);
            System.out.println(ret.getResponse().toString());

    }

    @Override
    public Map getOnePush(String clientId, String title, String text) {
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        TransmissionTemplate template = getTemplate(text);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(clientId);
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        return ret.getResponse();
    }

    public TransmissionTemplate getTemplate(String text) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(text);
        template.setTransmissionType(2);
        APNPayload payload = new APNPayload();
        payload.setBadge(1);
        payload.setContentAvailable(1);
        payload.setSound("default");
        payload.setCategory("$由客户端定义");
        payload.setAlertMsg(new APNPayload.SimpleAlertMsg(text));
        //字典模式使用下者
        //payload.setAlertMsg(getDictionaryAlertMsg());
        template.setAPNInfo(payload);
        return template;
    }
    private APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(){
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody("body");
        alertMsg.setActionLocKey("ActionLockey");
        alertMsg.setLocKey("LocKey");
        alertMsg.addLocArg("loc-args");
        alertMsg.setLaunchImage("launch-image");
        // IOS8.2以上版本支持
        alertMsg.setTitle("Title");
        alertMsg.setTitleLocKey("TitleLocKey");
        alertMsg.addTitleLocArg("TitleLocArg");
        return alertMsg;
    }

}
